package org.teachertools.web.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.teachertools.services.exception.NoResultException;
import org.teachertools.web.forms.ReportForm;

/**
 * @author eveno
 */
public class ReportAction extends CommonAction {

	@Override
	protected ActionForward process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ReportForm reportForm = (ReportForm) form;
		try {
			getServiceLocator().getReportService().generateReport(
					reportForm.getIdLivret(), reportForm.getEleveIds(),
					reportForm.getExportPath());
		} catch (NoResultException e) {
			log.warn(e.getMessage(), e);
			ActionMessages messages = new ActionMessages();
			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					"error.notes.notfound"));
			saveErrors(request, messages);
		}

		addLivretList(request);
		addClassList(request);
		addEleveList(request);

		ActionMessages messages = new ActionMessages();
		messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
				"success.do", getMessage(request, "libelle.generation")));
		saveMessages(request, messages);

		return mapping.findForward("continue");
	}
}
